package com.mairuide._frame.poiUtils;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

public class PoiZGBankXLS {

    public static List<Map<String, String>> poiImport(MultipartFile file) {
        Workbook wb =null;
        Sheet sheet = null;
        Row row = null;
        List<Map<String,String>> list = null;
        String cellData = null;

        //中国银行.xls
        String columns1[] = {"交易日期","交易账号","交易流水号","交易类型","交易时间","交易金额",
                "交易机构号","交易机构名","附言","付款人","付款账号","付款账户机构名","付款账户机构号",};
        String columns2[] = {"交易码","交易货币",
                "借贷标识","冲正标识","交易柜员","交易后余额","备注","用途","摘要","收款人",
                "收款账号","收款所属机构名","收款账户机构号"};
//
//        String columns1[] =new String[40];
//        String columns2[] =new String[50];

        wb = readExcel(file);
        if(wb != null){
            //用来存放表中数据
            list = new ArrayList<Map<String,String>>();
            //获取第一个sheet
            sheet = wb.getSheetAt(0);
            //获取最大行数
            int rownum = sheet.getPhysicalNumberOfRows();
            //获取第一行
            row = sheet.getRow(0);
            //获取最大列数
            int colnum = row.getPhysicalNumberOfCells();
            boolean flag = true;
            Map<String,String> map = new LinkedHashMap<String,String>();
            for (int i = 0; i<rownum; i++) {
                row = sheet.getRow(i);
                if(row == null) {
                    list.add(map);
                    flag = true;
                   map = new LinkedHashMap<String,String>();
                    continue;
                }
                for (int j = 0;j<colnum;j++){
                    cellData = (String) getCellFormatValue(row.getCell(j));
                    if(cellData.equals("交易码")) {
                        flag = false;
                        break;
                    }else if(cellData.equals("交易日期")) {
                        break;
                    }
                    if(flag) {
                        map.put(columns1[j], cellData);
                    }else {
                        map.put(columns2[j], cellData);
                    }
                }
            }
        }
        if (list!=null&&list.size()>0){
            //遍历解析出来的list
            for (Map<String,String> map : list) {
                for (Entry<String,String> entry : map.entrySet()) {
                    System.out.print(entry.getKey()+"\t"+entry.getValue()+"\t");
                }
                System.out.println();
            }
        }
        return list;
    }
    //读取excel
    public static Workbook readExcel(MultipartFile file){
        Workbook wb = null;
        if(file==null){
            return null;
        }
        String originalFilename = file.getOriginalFilename();
        String extString = originalFilename.substring(originalFilename.lastIndexOf("."));
        InputStream is = null;
        try {
            is = file.getInputStream();
            if(".xls".equals(extString)){
                return wb = new HSSFWorkbook(is);
            }else if(".xlsx".equals(extString)){
                return wb = new XSSFWorkbook(is);
            }else{
                return wb = null;
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return wb;
    }
    public static Object getCellFormatValue(Cell cell){
        Object cellValue = null;
        if(cell!=null){
            //判断cell类型
            switch(cell.getCellType()){
                case Cell.CELL_TYPE_NUMERIC:{
                    cellValue = String.valueOf(cell.getNumericCellValue());
                    break;
                }
                case Cell.CELL_TYPE_FORMULA:{
                    //判断cell是否为日期格式
                    if(DateUtil.isCellDateFormatted(cell)){
                        //转换为日期格式YYYY-mm-dd
                        cellValue = cell.getDateCellValue();
                    }else{
                        //数字
                        cellValue = String.valueOf(cell.getNumericCellValue());
                    }
                    break;
                }
                case Cell.CELL_TYPE_STRING:{
                    cellValue = cell.getRichStringCellValue().getString();
                    break;
                }
                default:
                    cellValue = "";
            }
        }else{
            cellValue = "";
        }
        return cellValue;
    }
}
